package com.czy.blog.controller;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.czy.blog.entity.Menu;
import com.czy.blog.service.MenuService;

@Controller
public class HomeController {
	@Autowired
	private MenuService menuService;
	
	@RequestMapping("/back")
	public String back(Model model){
		List<Menu> menus = menuService.findAll();
		Menu root = menuService.findRoot();
		
		//一级菜单
		List<Menu> firsts = new ArrayList<Menu>();
		for(Menu m : menus){
			if(m.getParent() != null && m.getParent().getId().equals(root.getId())){
				firsts.add(m);
			}
		}
		
		//设置一级菜单子菜单
		for(Menu f : firsts){
			Set<Menu> children = new HashSet<Menu>();
			for(Menu m : menus){
				if(m.getParent() != null && m.getParent().getId().equals(f.getId())){
					children.add(m);
				}
			}
			f.setChildren(children);
			
		}
		model.addAttribute("menus", firsts);
		return "/home/back";
	}
}
